package com.jia;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @Classname ListViewActivity
 * @Version v1.0
 * @Date 13-10-16 下午9:25
 * @Author jia.xiuya
 * @Copyright © 2003-2013 ZTEsoft Corporation. All rights reserved.
 */
public class ListViewActivity extends ListActivity implements AdapterView.OnItemClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null,
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
        String[] cols = new String[]{ContactsContract.Contacts.DISPLAY_NAME};
        int[] views = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                cursor, cols, views);
        this.setListAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.v("ListViewActivity", "in onItemClick with " + ((TextView) view).getText()
                +
                ". Position = " + i + ". Id = " + l);
        Uri selectedPerson = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, l);
        Intent intent = new Intent(Intent.ACTION_VIEW, selectedPerson);
        startActivity(intent);
    }


}
